//
//  SQLClass.m
//  sqlite
//
//  Created by 崔博 on 2018/9/25.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "SQLClass.h"
#import <sqlite3.h>
@implementation SQLClass

// 第一步创建db
+ (void)creatDBFile
{
    NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
    NSString * dbFile = [filePath stringByAppendingPathComponent:@"eco.db"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dbFile]) {
        if (![[NSFileManager defaultManager] createFileAtPath:dbFile contents:nil attributes:nil]) {
            NSLog(@"创建成功");
        }else
        {
            NSLog(@"创建失败");
        }
    }else
    {
        NSLog(@"存在了");
    }
}
static sqlite3 *dataBase;

+ (void)creatDBfileSQL
{
    
    NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject];
    NSString * dbfilePath = [filePath stringByAppendingPathComponent:@"eco.db"];
    int status = sqlite3_open_v2([dbfilePath UTF8String], &dataBase, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FULLMUTEX, NULL);
    if (status != SQLITE_OK) {
        NSLog(@"%@",dbfilePath);
    }
}
// 创建表
+ (void)creatTableName:(NSString *)tableName
{
    //
    NSString * sqlStr = [NSString stringWithFormat:@"create table if not exists %@(id char, name, char)",tableName];
    // 检测语句
    sqlite3_stmt *stmt; // 代表一个指令
    int status = sqlite3_prepare(dataBase, [sqlStr UTF8String], -1, &stmt, 0);
    if (status != SQLITE_OK) {
        NSLog(@"sqlite_prepare_v2 fali");
    }
    // 执行指令
    status = sqlite3_step(stmt);
    if (status != SQLITE_DONE) {
        NSLog(@"sqlite_step fali");
    }
    // 结束
    sqlite3_finalize(stmt);
}
// 操作表

@end
